'''
Map 函数
- Map 函数是什么？
- Map 结合列表，元组，以及集合
- Map 内置的函数功能
- Map 的多个迭代器
- Map 的lambda 函数
'''

numbs = (1, 2, 3, 4, 5, 6)
result = []
for num in numbs:
    x = num * num
    result.append(x)
print(result)


# 转换一
def square(n):
    return n * n


nums_squared = map(square, numbs)
print(list(nums_squared))
# print(tuple(nums_squared))
# print(set(nums_squared))

# 转换二
nums_list = [1, 2, 3, 4, 5, 6]
nums_tuple = (1, 2, 3, 4, 5, 6)
nums_set = {1, 2, 3, 4, 5, 6}


def double(n):
    return n * 2


result_list = map(double, nums_list)
result_tuple = map(double, nums_tuple)
result_set = map(double, nums_set)

# map 对象
print(result_list)
print(result_tuple)
print(result_set)

print(list(result_list))
print(tuple(result_tuple))
print(set(result_set))

# 转换三 结合内置函数使用
numbs2 = [1, 2, 3, -4, 5, -6]
numbs2_list = list(map(abs, numbs2))
print(numbs2_list)

words = ['how', 'are', 'your', '?']
words_list = list(map(len, words))
print(words_list)

# map 结合多个迭代器使用
beginging = ['bl', 'gr', 're']
end = ['ue', 'een', 'd']


def concatenate(string_1, string_2):
    return string_1 + string_2


concatenated = map(concatenate, beginging, end)
print(concatenated)
print(list(concatenated))

# map 结合lambda 使用
numbs_lambda = (1, 2, 3, 4, 5, 6)
print(list(map(square, numbs_lambda)))
# lambda
result = map(lambda n: n * n, numbs_lambda)  # 将square 函数折成了一行
print(list(result))
